from wxauto import *
import time
import json
import requests
from DrissionPage import ChromiumPage

# 获取当前微信客户端
wx = WeChat()
page = ChromiumPage()

# 目标用户名
target_user = '@空小宝'


def create_jira_issue(summary,description):
  url = "https://worklist.linkingopen.com/rest/api/2/issue/"
  payload = json.dumps({
    "fields": {
        "project": {
          "key": "DTWIN"
        },
        "summary": summary,
        "description": description,
        "issuetype": {
          "name": "任务"
        }
    }
  })
  headers = {
    'Authorization': 'Basic amlhd2VpLm06QWNjcDEyMzQ=',
    'Content-Type': 'application/json'
  }

  response = requests.request("POST", url, headers=headers, data=payload)
  body = json.loads(response.text)
  print(response.text)
  return body['key']


while True:
  msgs = wx.GetAllNewMessage()
  # 遍历内容
  for key, messages in msgs.items():
    for message in messages:
      sender = message[0]
      if sender == 'SYS':
        print(message)
        continue
      msg = message[1]
      if target_user in msg:
        print(f"在 {key} 中找到 {sender} 发送的消息: {msg}")
        # tab = page.new_tab()
        # tab.get('https://worklist.linkingopen.com/secure/Dashboard.jspa')
        # time.sleep(3)
        # create_button = tab.ele('.aui-header aui-dropdown2-trigger-group').ele('#create_link')
        # #print(create_button)
        # create_button.click()
        # # 创建工单
        # time.sleep(5)
        desc = '群名：'+key  +',发送人'+sender+ ',内容:' + msg
        # tab.ele('.summary').input(desc)
        # tab.ele('.description').input(desc)
        # tab.ele('#create-issue-submit').click()
        # issue_href = tab.ele('.aui-message closeable aui-message-success aui-will-close').ele('.issue-created-key issue-link').attr('href')
        # print(issue_href)
        issue_id = create_jira_issue(msg,desc)
        issue_href = 'https://worklist.linkingopen.com/browse/' + issue_id

        # 发送消息
        who = '毛佳伟🐳'
        notice_msg = '您有一个新工单:'+issue_href
        wx.SendMsg(notice_msg, who)

